package com.kingmed.kmss.common.service.sys.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.kingmed.kmss.common.exception.BusinessException;
import com.kingmed.kmss.common.service.sys.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * 文件服务实现类(unused)
 */
@Service
@Slf4j
public class FileServiceImpl implements FileService {


    /**
     * resources文件下载并反馈给前端
     *
     * @param filePathAndName 文件保存路径+文件名称
     * @param type            type=0下载后浏览器直接打开,type=1下载
     */
    @Override
    public void resourcesFileDownload(HttpServletRequest request, HttpServletResponse response, String filePathAndName, Integer type) throws IOException {
        try {
            InputStream inputStream = this.getClass().getResourceAsStream(filePathAndName);

            // 反馈给前端
            BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            if (inputStream == null) {
                return;
            }
            // 为防止 文件名出现乱码
            final String userAgent = request.getHeader("USER-AGENT");
            // IE浏览器
            if (StrUtil.contains(userAgent, "MSIE")) {
                filePathAndName = URLEncoder.encode(filePathAndName, "UTF-8");
            } else {
                // google、火狐浏览器
                if (StrUtil.contains(userAgent, "Mozilla")) {
                    filePathAndName = new String(filePathAndName.getBytes(), "ISO8859-1");
                } else {
                    // 其他浏览器
                    filePathAndName = URLEncoder.encode(filePathAndName, "UTF-8");
                }
            }
            // 设置让浏览器弹出下载提示框，而不是直接在浏览器中打开, type==0下载后浏览器直接查看
            response.setContentType("application/" + (type == 0 ? "octet-stream" : "x-download"));
            // 文件obs存储名称 毫秒时间戳 + "_" + 文件名称
            String fileName = filePathAndName.substring(filePathAndName.indexOf("/") + 1);
            response.addHeader("Content-Disposition", (type == 0 ? "inline" : "attachment") + ";filename=" + fileName);

            IoUtil.copy(inputStream, outputStream);
        } catch (Exception e) {
            log.error("obs文件下载失败", e);
            throw new BusinessException("obs文件下载失败：" + e.getMessage());
        }
    }

    /**
     * 模版文件下载并反馈给前端
     *
     * @param request
     * @param response
     * @param type
     * @param lst
     * @param clazz
     * @throws IOException
     */
    @Override
    public void templateFileDownload(HttpServletRequest request, HttpServletResponse response, Integer type, List<?> lst, Class clazz) throws IOException {
        try {
//            InputStream inputStream = this.getClass().getResourceAsStream(filePathAndName);

            // 反馈给前端
            BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
//            if (inputStream == null) {
//                return;
//            }
            // 为防止 文件名出现乱码
            final String userAgent = request.getHeader("USER-AGENT");
            // IE浏览器
//            if (StrUtil.contains(userAgent, "MSIE")) {
//                filePathAndName = URLEncoder.encode(filePathAndName, "UTF-8");
//            } else {
//                // google、火狐浏览器
//                if (StrUtil.contains(userAgent, "Mozilla")) {
//                    filePathAndName = new String(filePathAndName.getBytes(), "ISO8859-1");
//                } else {
//                    // 其他浏览器
//                    filePathAndName = URLEncoder.encode(filePathAndName, "UTF-8");
//                }
//            }
            // 设置让浏览器弹出下载提示框，而不是直接在浏览器中打开, type==0下载后浏览器直接查看
            response.setContentType("application/" + (type == 0 ? "octet-stream" : "x-download"));
            // 文件obs存储名称 毫秒时间戳 + "_" + 文件名称
//            String fileName = filePathAndName.substring(filePathAndName.indexOf("/") + 1);
//            response.addHeader("Content-Disposition", (type == 0 ? "inline" : "attachment") + ";filename=" + fileName);
//            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
//            ExcelWriter excelWriter  = EasyExcel.write(arrayOutputStream,clazz).build();
//            WriteSheet writeSheet = EasyExcel.writerSheet().build();
//            excelWriter.write(lst, writeSheet);
//            excelWriter.finish();
//            IoUtil.copy(inputStream, outputStream);
        } catch (Exception e) {
            log.error("obs文件下载失败", e);
            throw new BusinessException("obs文件下载失败：" + e.getMessage());
        }
    }


}